// pages/mySelfInfomation/mySelfInfomation.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    realName: '',
    mobile: '',
    mobile2: '',
    addr: '',
    job1: '',
    job2: '',
    job1Index: '',
    job2Index: '',
    codeIn: '',
    idCardZ: '../../image/info/sfz-z@2x.png',
    idCardF: '../../image/info/sfz-f@2x.png',
    cert1: '',
    cert2: '',
    show: '',
    certs: [],
    jobArray: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.request({
      url: app.globalData.prefix + '/memberAction/memberInfo.do',
      data: {
        openId: app.globalData.userInfo.openId
      },
      success: (res) => {
        let status = res.data.status
        if (status === '00') {
          let data = res.data.result
          this.data.userInfo.nickName = data.nickName
          this.data.userInfo.avatarUrl = data.avatarUrl
          this.setData({
            userInfo: this.data.userInfo,
            realName: data.realName,
            mobile: data.mobile,
            mobile2: data.mobile2,
            addr: data.addr,
            job1: data.job1,
            job2: data.job2,
            codeIn: data.codeIn,
            cert1: data.cert1,
            cert2: data.cert2,
            certs: data.certs,
            show: data.show
          })
          wx.request({
            url: app.globalData.prefix + '/memberAction/job.do',
            data: {},
            success: (res) => {
              let status = res.data.status
              if (status === '00') {
                let data = res.data.result
                this.setData({
                  jobArray: data
                })
                this.data.jobArray.forEach((element, index) => {
                  if (element.id === this.data.job1) {
                    this.setData({
                      job1Index: index
                    })
                  }
                  if (element.id === this.data.job2) {
                    this.setData({
                      job2Index: index
                    })
                  }
                })
              } else {
                wx.$showToast(res.data.errorMsg)
              }
            }
          })
        } else {
          wx.$showToast(res.data.errorMsg)
        }
      }
    })
  },
  completeInfo() {
    wx.navigateTo({
      url: '../completeSelfInfo/completeSelfInfo?data='+JSON.stringify(this.data)
    })
  }
})